﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace XcoAppSpaces.Core.Ports.Utils
{
	/// <summary>
	/// This class adds extension methods to the port that are needed at the appspace for working with local and remote ports.
	/// </summary>
	public static class PortHelpers
	{
		/// <summary>
		/// Gets the message types of a portset.
		/// </summary>
		/// <param name="portSet">The portset.</param>
		/// <returns>The types of messages that can be posted to the portset.</returns>
		public static Type[] GetMessageTypes(this IPortSet portSet)
		{
			return portSet.Ports.Select(p => p.MessageType).ToArray();
		}

		/// <summary>
		/// Gets the message type from a given type that inherits from Port
		/// </summary>
		/// <param name="portType">The port type</param>
		/// <returns>The message type of the port, or null if no message type could be found</returns>
        public static Type GetMessageType(Type portType)
        {
        	if (portType == null)
        		return null;

			return IsPortType(portType) ?
				portType.GenericTypeArguments.First() :
				GetMessageType(portType.GetTypeInfo().BaseType);
        }

		/// <summary>
		/// Gets the message type from a given type that inherits from any of the PortSet types
		/// </summary>
		/// <param name="portSetType">The port set type.</param>
		/// <returns>The message types of the portset, or null if no message types could be found.</returns>
		public static Type[] GetMessageTypes(Type portSetType)
		{
			if (portSetType == null)
				return null;

			return IsPortOrPortSetType(portSetType) ?
				portSetType.GenericTypeArguments :
				GetMessageTypes(portSetType.GetTypeInfo().BaseType);
		}

		private static bool IsPortType(Type type)
		{
			return (type != null && type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Port<>));
		}

		private static bool IsPortOrPortSetType(Type type)
		{
			return (type != null && type.IsConstructedGenericType &&
				(type.GetGenericTypeDefinition() == typeof(Port<>) ||
				type.GetGenericTypeDefinition().Name.StartsWith("PortSet"))); //cannot compare to a single type here because there are many different generic portSet types - therefore just using the type name here
		}
	}
}
